/* ***********************
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is oisterBake.js module.
 *
 * The Initial Developer of the Original Code is Rob Gerns.
 *
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 **************************/
function oisterBake(oisterURL) {
    /****************************************************
    * Function Name: oisterBake
    *
    * Purpose:
    * Use AJAX to grab the XML file, call the "createSkeleton"
    * function to set up a basic structure on the HTML page 
    * before going on to do anything else.  Call the 
    * handleResponse() function only after the XML file is completely
    * received and the skeleton structure is ready.
    *****************************************************/  
    var objContainer = document.getElementById("container");
    var objErrMsg = "";
    var objErrMsgId = "";
    var objHttpRequest = getHttpObject();
    var strOisterURL = document.getElementById(oisterURL).value;
    
    //If a "container" node is left over in the UI HTML, remove it.
    if (objContainer !== null && objContainer !== undefined) {
        clearContainer();
    }  
    if (strOisterURL === "" || 
            strOisterURL === "Please enter a URL in this space first.") {
        strOisterURL.value = "Please enter a URL in this space first.";
    }
    else {  
        if (strOisterURL.indexOf('http://') === -1) {
            strOisterURL = "http://" + strOisterURL;
        }
        //start an AJAX GET request for the Oister XML	
        objHttpRequest.open("GET", strOisterURL, true);
        //Call the createSkelton function to generate necessary 'div' layers.
        createSkeleton();  
        //When the XML data is ready, call the handleResponse function.
        objHttpRequest.onreadystatechange = function() {
        if (objHttpRequest.readyState === 4 && 
                    objHttpRequest.status === 200) { 
                handleResponse(objHttpRequest);
        }
        else {
            if (objHttpRequest.readyState === 4) {
                document.getElementById("begin").style.display="none";
                objErrMsg = document.createElement("h1");
                objErrMsgId = objErrMsg.id = "errormsg";
                document.getElementById("container").appendChild(objErrMsg);
                objErrMsg.innerHTML = "An Oister feed wasn't found at that URL.";
                objErrMsg.innerHTML += " The URL may have changed.";
            }
            }
        }
        //Nothing to send back to the server.
        objHttpRequest.send(null);	
        return true; 
    } 
}  